module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema,
        ObjectId = Schema.Types.ObjectId;
  /**
   * @ role data schema
   */
  let RoleSchema = new Schema({
    name: String,
    value: Number,
    status: { type: Number, default: 1 },
    menus: [{ type: ObjectId, ref: 'Menu'}],
    permissions: [{ type: String, default: '*' }],
    remark: String,
    date: { type: Number, default: Date.now }
  }, {usePushEach: true});

  RoleSchema.statics = {
    add(data) {
      return data.save();
    },
    update(id, data) {
      return this.findByIdAndUpdate({_id: id}, {$set: data}, {new: true});
    },
    findAll(cb) {
      return this.find({}).populate({path: 'menus'}).exec(cb);
    },
    findById(id) {
      return this.findOne({ _id: id })
          .populate({path: 'menus'});
    },
    del(id) {
      return this.remove({ _id: id }).exec();
    }
  };

  return mongoose.model('Role', RoleSchema);
}